<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/8/22 14:38
 * Note:
 */
namespace Admin\Model;
use Think\Model;

class MenuModel extends Model
{
    // 获取用户的快捷菜单
    public function getQuickMenus($uid = 0)
    {
        if (!$uid) return false;
        return M('quick_menu')
            ->alias('qm')
            ->field('m.*')
            ->join('__MENU__ m ON m.id = qm.menu_id')
            ->where(array('qm.uid'=>$uid,'m.type'=>0,'qm.scene'=>0))
            ->order('qm.id desc')
            ->select();
    }

    // 根据MVC获取菜单信息
    public function getInfoByUrl($url = ''){
        if (!$url) return array('id'=>0,'pid'=>0);
        $menu = $this->where(array('url'=>$url))->find();
        return $menu?:array('id'=>0,'pid'=>0);
    }

    // 根据权限组ID，获取菜单
    public function getMenus($group_id = 0){
        $menu_ids = M('auth_group')->where(array('id'=>$group_id))->getField('menu_ids');
        if ($menu_ids){
            $menus  = $this->where(array('id'=>array('in',$menu_ids),'scene'=>0))->order('sort asc')->select();
            $return = array();
            if ($menus){
                foreach ($menus as $k=>$menu){
                    if ($menu['pid'] == 0){
                        $return[$menu['id']] = $menu;
                        if (!isset($return[$menu['id']]['child'])){
                            $return[$menu['id']]['child'] = array();
                        }
                    }
                }
                foreach ($menus as $menu){
                    if ($menu['pid'] != 0){
                        $return[$menu['pid']]['child'][] = $menu;
                    }
                }
            }
            return $return;
        } else {
            return array();
        }
    }
}